package com.lk.v1;

import com.lk.base.mysql.domain.TblWrjData;
import com.lk.device.dto.DeviceDTO;
import com.lk.device.service.DevicesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

@Controller
@RequestMapping("/hub/v1")
public class DeviceController {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private DevicesService devicesService;


    @PostMapping("/device/list")
    @ResponseBody
    public ResponseEntity deviceList(DeviceDTO dto) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        Pageable pageable = PageRequest.of(dto.getPageNumber(), dto.getPageSize(),  Sort.by(Sort.Direction.DESC, "createTime"));
        Object o = devicesService.selectAll(dto, pageable);
        return new ResponseEntity(o, HttpStatus.OK);
    }

    @PostMapping("/device/wrj")
    @ResponseBody
    public ResponseEntity wrj(DeviceDTO dto) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        dto.setDeviceType("无人机");
        Object o = devicesService.selectOne(dto);
        return new ResponseEntity(o, HttpStatus.OK);
    }

    @PostMapping("/device/dc")
    @ResponseBody
    public ResponseEntity dc(DeviceDTO dto) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        dto.setDeviceType("吊舱");
        Object o = devicesService.selectOne(dto);
        return new ResponseEntity(o, HttpStatus.OK);
    }
    @PostMapping("/device/jk")
    @ResponseBody
    public ResponseEntity jk(DeviceDTO dto) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        dto.setDeviceType("机库");
        Object o = devicesService.selectOne(dto);
        return new ResponseEntity(o, HttpStatus.OK);
    }
    @PostMapping("/device/sendWrjData")
    @ResponseBody
    public ResponseEntity sendWrjData(TblWrjData dto) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        return new ResponseEntity(devicesService.sendWrjData(dto), HttpStatus.OK);
    }



}
